﻿using System;
using System.Windows;
using Convertor.Interface;
using Wpf.CommonTools.SystemExtension;
using System.Linq;

namespace Convertor.Plugin.DispatchTemplate
{
    public class Plugin : PluginBase, IPluginOutputHeler, IPluginLoadPluginItems
    {
        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.DispatchTemplate;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.DispatchTemplate;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_System") + " - " + UILanguage.FindResource("Plugin_DispatchTemplate_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_DispatchTemplate_Desc"); }
        }

        #endregion

        #region IConvertParamters 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error | LogTypes.Normal); }
                content.StopConvert = true;
                return content;
            }

            if (Log != null) { Log(this, config.ToolTip, LogTypes.Normal); }


            if (string.IsNullOrEmpty(config.UniqueId))
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_OpenConfigForm"), LogTypes.Normal | LogTypes.HidePluginName); }
                if (Loader.ShowDialog() == true)
                {
                    config = ((IFormGetConfigEvent)Loader.Loader).GetConfig() as ConfigContent;
                }
                else
                {
                    if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_CloseConfigForm"), LogTypes.Error | LogTypes.HidePluginName); }
                    content.StopConvert = true;
                    return content;
                }
            }

            if (string.IsNullOrEmpty(config.UniqueId))
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_DispatchTemplate_SeletedTemplateEmpty"), LogTypes.Error | LogTypes.HidePluginName); }
                return content;
            }

            var pluginItems = LoadPluginItems(config.UniqueId);
            if (pluginItems == null)
            {
                if (Log != null) { Log(this, UILanguage.TryFindResource("Plugin_DispatchTemplate_Failure"), LogTypes.Error | LogTypes.HidePluginName); }
                content.StopConvert = true;
                return content;
            }
            else
            {
                if (Log != null) { Log(this, UILanguage.TryFindResource("Plugin_DispatchTemplate_Success"), LogTypes.Normal | LogTypes.HidePluginName); }
                IConvertParamters output = OutputFunc.OutputFunc(content, pluginItems);
                return output;
            }
        }

        #endregion

        #region IPluginConfigForm 成员

        public override PluginConfigForm ConfigForm
        {
            get { return new ConfigForm(); }
        }

        #endregion

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) ?? new ConfigContent(); }
        }

        #endregion

        #region IPluginOutputHeler 成员

        public IConvertHelper OutputFunc { get; set; }

        #endregion

        #region IPluginLoadPluginItems 成员

        public LoadPluginItemsDelegate LoadPluginItems { get; set; }

        #endregion
    }
}
